<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visible"
    :title="id ? '修改分类' : '新增分类'"
    @close="resetForm"
    width="40%"
  >
    <el-form v-loading="editLoading" ref="formRef" :model="formData" :rules="formRules" label-width="80px">
      <div class="px-4">
        <el-form-item prop="name" label="分类名称">
          <el-input v-model="formData.name" placeholder="请输入分类名称" maxlength="50" />
        </el-form-item>
        <el-form-item prop="type" label="分类类型">
          <el-radio-group v-model="formData.type" class="ml-4">
            <el-radio :label="1">目录</el-radio>
            <el-radio :label="2">应用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="status" label="分类状态">
          <el-radio-group v-model="formData.status" class="ml-4">
            <el-radio :label="0">显示</el-radio>
            <el-radio :label="1">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="sort" label="显示顺序">
          <el-input-number v-model="formData.sort" :min="1" :max="100" placeholder="请输入显示排序" />
        </el-form-item>
      </div>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleCreate" :loading="submitBtnLoading">确认</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
import { queryParameterDetail } from '@/api/system/parameter'
import { addAppType, appTypeInfo, updateAppType } from '@/api/system/app'
import { ElMessage, ElMessageBox } from 'element-plus'

const props = defineProps({
  id: {
    type: String,
    default: ''
  },
  visible: {
    type: Boolean,
    default: false
  },
  parentId: {
    type: String,
    default: ''
  }
})
const emit = defineEmits(['update:visible', 'create', 'update'])
const formRef = ref(null)
const formData = reactive({
  name: '',
  type: 1,
  status: 0,
  sort: ''
})
const formRules = reactive({
  name: [{ required: true, trigger: 'blur', message: '请输入分类名称' }]
})
const handleCreate = () => {
  formRef.value?.validate((valid) => {
    if (valid) {
      submitBtnLoading.value = true
      if (!props.id) {
        addAppType(formData)
          .then(() => {
            ElMessage.success('新增成功')
            emit('addSuccess')
          })
          .finally(() => {
            submitBtnLoading.value = false
          })
      } else {
        updateAppType(formData)
          .then(() => {
            ElMessage.success('修改成功')
            emit('addSuccess')
          })
          .finally(() => {
            submitBtnLoading.value = false
          })
      }
    } else {
      return false
    }
  })
}
const resetForm = () => {
  emit('update:visible')
  formRef.value.resetFields()
}
const submitBtnLoading = ref(false)
const editLoading = ref(false)
watch(
  () => props.visible,
  async (newValue, oldValue) => {
    if (newValue) {
      const { id, parentId } = props
      if (id) {
        editLoading.value = true
        const { data } = await appTypeInfo(id)
        Object.assign(formData, data)
        editLoading.value = false
      } else {
        formData.parentId = parentId
      }
    }
  }
)
defineExpose({
  submitBtnLoading
})
</script>
<style lang="scss" scoped></style>
